<?php

class _F_EXTENSION_USERS_ extends _F_EXTENSION_ {
	
	// settings
	public $use_email_as_login_name = true;
	public $enable_account_after_registration = true;

	public $db;
	public $prev_route;
	public $profile_fileds = array();

	public function __construct() {
		$dir = dirname(__FILE__);
		require_once $dir.'/user.model.php';
		require_once $dir.'/role.model.php';
		f()->template_path_list[] = $dir.'/templates';
		f()->register_method('users',array($this,'users'));
		if (f()->has_method('db')) $this->db = f()->db();
		$this->prev_route = f()->register_method(
			'route',array($this,'route'));		
	}
	
	public function route() {
		$r = f()->request;
		if ($r === '/users' || strpos($r,'/users/') === 0) {
			f()->_f_route_(substr($r,6),'/extensions/users/actions');
		} else {
			call_user_func($this->prev_route);
		}
	}

	public function users() {
		return $this;
	}
		
	public function user($pkey = null) {
		if ($pkey === null) return $this->db->create('user','_F_MODEL_USER_');
		return $this->db->find_by_pkey('user','_F_MODEL_USER_',$pkey);
	}
		
	public function role($pkey = null) {
		if ($pkey === null) return $this->db->create('role','_F_MODEL_ROLE_');
		return $this->db->find_by_pkey('role','_F_MODEL_ROLE_',$pkey);
	}
		
	public function permition($pkey = null) {
		if ($pkey === null) return $this->db->create('permition','_F_MODEL_PERMITION_');
		return $this->db->find_by_pkey('permition','_F_MODEL_PERMITION',$pkey);
	}

	public function find_all_users() {
		return $this->db->find_all('user','_F_MODEL_USER_');
	}

	public function find_all_roles() {
		return $this->db->find_all('role','_F_MODEL_ROLE_');
	}
	
	public function find_all_permitions() {
		return $this->db->find_all('permition','_F_MODEL_PERMITION_');
	}
	
	public function user_form($type = 'login') {
		switch ($type) {
			case 'password':
				$title = t('Change Password');
				$button_label = t('Change password');
				break;
			case 'register': 
				$title = t('Registration');
				$button_label = t('Register');
				break;
			case 'add': 
				$title = t('Add user');
				$button_label = t('Save');
				break;
			case 'edit': 
				$title = t('Edit user profile');
				$button_label = t('Save');
				break;
			case 'logout': 
				$title = null;
				$button_label = t('Sign out');
				break;
			default: // login
				$title = null;
				$button_label = t('Sign in');
				break;
		}
		$form = f()->form('f_user_form_'.$type,array('title' => $title));
		if ($type == 'login' || $type == 'logout' || $type == 'password') {
			$login_group = $form;
		} else {
			$login_group = $form->group('login_information',t('Login information'));
		}
		if ($type != 'logout' && $type != 'password') {
			$login_group->text('id',t('Login name'),
				array(
					'size' => 'small', 
					'disabled' => $type != 'login' && $type != 'register' &&
						$type != 'add',
				));
		}			
		if ($type == 'password' || $type == 'add') {
			$login_group->password('current_password',t('Current passoword'),array(
				'attr' => array(
					'autocomplete' => $type == 'login' ? 'on' : 'off',
				),
			));
		}
		if ($type == 'register' || $type == 'password' || $type == 'login' || $type == 'add') {
			$login_group->password('password1',t('Passoword'),array(
				'attr' => array(
					'autocomplete' => $type == 'login' ? 'on' : 'off',
				),
			));
		}
		if ($type == 'register' || $type == 'password') {
			$login_group->password('password2',t('Retype your password'),array(
				'attr' => array(
					'autocomplete' => 'off',
				),
			));
		}
		if ($type != 'password') {
			if ($type == 'add' || $type == 'edit') {
				$login_group->radio('user_state',t('State','user_state'),array(
					'ENABLED' => 'Enabled',
					'DISABLED' => 'Disabled',
				));
			}
			if ($type != 'login' && $type != 'logout') {
				$personal_group = $form->group('personal_information',
					t('Personal information'));
				$personal_group->text('first_name',t('First name'),array('size' => 'small'))
					->text('last_name',t('Last name'),array('size' => 'small'))
					->radio('gender',t('Gender'),array('MALE' => t('Male'), 
						'FEMALE' => t('Female')));
				$address_group = $form->group('address_information',t('Address'));
				$address_group->text('address',t('Address'),array('rows' => 2))
					->text('city',t('City'))
					->text('state',t('State'))
					->text('country',t('Country'))
					->text('zip',t('Zip code'),array('size' => 'small'));
				$contatct_group = $form->group('contact_information',t('Contact information'));
				if (!$this->use_email_as_login_name) 
					$contact_information->text('email',t('Email address'));
				$contatct_group->text('phone',t('Phone'))
					->text('mobile',t('Mobile'));
				// permitions
				// TODO:
			}
		}
		$form->button('submit',$button_label);
		return $form;
	}

	public function role_form($type = 'add') {
		switch ($type) {
			case 'edit': 
				$title = t('Edit role');
				break;
			default: // add
				$title = t('Add role');
				break;
		}
		return f()->form('f_role_form_'.$type,array('title' => $title))
			->text('id',t('Name'),
				array(
					'size' => 'medium', 
					'disabled' => $type != 'add',
				))
			->text('description',t('Description'),
				array(
					'rows' => '5', 
				))
			->button('submit',t('Save'));
	}



}

new _F_EXTENSION_USERS_();
